<template>
  <div class="bpm-form-container">
    <!-- 文档提示 -->
    <div class="doc-alert-wrapper">
      <doc-alert title="工作流手册" url="https://doc.iocoder.cn/bpm/" class="enhanced-doc-alert" />
    </div>

    <!-- 搜索区域 -->
    <ContentWrap class="search-wrapper">
      <!-- 搜索头部 -->
      <div class="search-header">
        <div class="header-icon">
          <Icon icon="ep:document" />
        </div>
        <div class="header-content">
          <h3 class="search-title">表单管理</h3>
          <p class="search-subtitle">管理工作流程中的表单配置</p>
        </div>
      </div>

      <!-- 搜索表单 -->
      <div class="search-form-container">
        <el-form
          ref="queryFormRef"
          :inline="true"
          :model="queryParams"
          label-width="100px"
          label-position="left"
          class="enhanced-search-form"
        >
          <div class="form-grid">
            <el-form-item label="表单名" prop="name" class="form-item">
              <el-input
                v-model="queryParams.name"
                clearable
                placeholder="请输入表单名"
                @keyup.enter="handleQuery"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:document" />
                </template>
              </el-input>
            </el-form-item>
          </div>

          <div class="form-actions">
            <el-button @click="handleQuery" class="search-btn">
              <Icon icon="ep:search" class="btn-icon" />
              搜索
            </el-button>
            <el-button @click="resetQuery" class="reset-btn">
              <Icon icon="ep:refresh" class="btn-icon" />
              重置
            </el-button>
            <el-button
              v-hasPermi="['bpm:form:create']"
              plain
              type="primary"
              @click="openForm"
              class="create-btn"
            >
              <Icon icon="ep:plus" class="btn-icon" />
              新增
            </el-button>
          </div>
        </el-form>
      </div>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap class="table-wrapper">
      <!-- 表格头部 -->
      <div class="table-header">
        <div class="header-icon">
          <Icon icon="ep:list" />
        </div>
        <div class="header-content">
          <h4 class="table-title">表单列表</h4>
          <p class="table-subtitle">共 {{ total }} 个表单</p>
        </div>
      </div>

      <!-- 表格容器 -->
      <div class="table-container">
        <el-table
          v-loading="loading"
          :data="list"
          border
          class="enhanced-table"
          :header-cell-style="{ background: '#f8f9ff', color: '#303133', fontWeight: '600' }"
        >
          <el-table-column align="center" label="编号" prop="id" width="100">
            <template #default="scope">
              <div class="id-cell">
                <Icon icon="ep:key" class="id-icon" />
                <span class="id-text">{{ scope.row.id }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column align="center" label="表单名" prop="name">
            <template #default="scope">
              <div class="name-cell">
                <Icon icon="ep:document" class="name-icon" />
                <span class="name-text">{{ scope.row.name }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column align="center" label="状态" prop="status" width="100">
            <template #default="scope">
              <div class="status-cell">
                <dict-tag
                  :type="DICT_TYPE.COMMON_STATUS"
                  :value="scope.row.status"
                  class="enhanced-dict-tag"
                />
              </div>
            </template>
          </el-table-column>

          <el-table-column align="center" label="备注" prop="remark" min-width="200">
            <template #default="scope">
              <div class="remark-cell">
                <Icon icon="ep:chat-line-round" class="remark-icon" />
                <el-tooltip :content="scope.row.remark" placement="top">
                  <span class="remark-text">{{ scope.row.remark || '-' }}</span>
                </el-tooltip>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            :formatter="dateFormatter"
            align="center"
            label="创建时间"
            prop="createTime"
            width="200"
          >
            <template #default="scope">
              <div class="time-cell">
                <Icon icon="ep:clock" class="time-icon" />
                <span>{{ dateFormatter(scope.row, null, scope.row.createTime) }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column align="center" label="操作" width="240" fixed="right">
            <template #default="scope">
              <div class="action-buttons">
                <el-button
                  v-hasPermi="['bpm:form:update']"
                  link
                  type="primary"
                  @click="openForm(scope.row.id)"
                  class="action-btn edit-btn"
                  size="small"
                >
                  <Icon icon="ep:edit" class="btn-icon" />
                  编辑
                </el-button>
                <el-button
                  v-hasPermi="['bpm:form:query']"
                  link
                  @click="openDetail(scope.row.id)"
                  class="action-btn detail-btn"
                  size="small"
                >
                  <Icon icon="ep:view" class="btn-icon" />
                  详情
                </el-button>
                <el-button
                  v-hasPermi="['bpm:form:delete']"
                  link
                  type="danger"
                  @click="handleDelete(scope.row.id)"
                  class="action-btn delete-btn"
                  size="small"
                >
                  <Icon icon="ep:delete" class="btn-icon" />
                  删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <Pagination
          v-model:limit="queryParams.pageSize"
          v-model:page="queryParams.pageNo"
          :total="total"
          @pagination="getList"
        />
      </div>
    </ContentWrap>

    <!-- 表单详情的弹窗 -->
    <Dialog v-model="detailVisible" title="表单详情" width="800" class="enhanced-dialog">
      <!-- 弹窗头部 -->
      <div class="dialog-header">
        <div class="header-icon">
          <Icon icon="ep:view" />
        </div>
        <div class="header-content">
          <h4 class="dialog-title">表单详情</h4>
          <p class="dialog-subtitle">查看表单的详细配置信息</p>
        </div>
      </div>

      <!-- 表单预览内容 -->
      <div class="form-preview-container">
        <form-create :option="detailData.option" :rule="detailData.rule" />
      </div>
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as FormApi from '@/api/bpm/form'
import { setConfAndFields2 } from '@/utils/formCreate'

defineOptions({ name: 'BpmForm' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const { currentRoute, push } = useRouter() // 路由

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: null
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await FormApi.getFormPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const openForm = (id?: number) => {
  const toRouter: { name: string; query?: { id: number } } = {
    name: 'BpmFormEditor'
  }
  // 表单新建的时候id传的是event需要排除
  if (typeof id === 'number') {
    toRouter.query = {
      id
    }
  }
  push(toRouter)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await FormApi.deleteForm(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 详情操作 */
const detailVisible = ref(false)
const detailData = ref({
  rule: [],
  option: {}
})
const openDetail = async (rowId: number) => {
  // 设置表单
  const data = await FormApi.getForm(rowId)
  setConfAndFields2(detailData, data.conf, data.fields)
  // 弹窗打开
  detailVisible.value = true
}
/**表单保存返回后重新加载列表 */
watch(
  () => currentRoute.value,
  () => {
    getList()
  },
  {
    immediate: true
  }
)
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

.bpm-form-container {
  padding: 16px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;

  @media (max-width: 768px) {
    padding: 8px;
  }
}

/* 文档提示样式 */
.doc-alert-wrapper {
  margin-bottom: 16px;

  .enhanced-doc-alert {
    border-radius: $border-radius;
    box-shadow: $shadow-light;
    border: 1px solid $border-light;
    overflow: hidden;

    :deep(.el-alert) {
      border-radius: $border-radius;
    }

    :deep(.el-alert__icon) {
      color: $primary-color;
    }

    :deep(.el-alert__title) {
      color: $text-primary;
      font-weight: 600;
    }
  }
}

/* 搜索区域样式 */
.search-wrapper {
  margin-bottom: 16px;
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;

  :deep(.el-card__body) {
    padding: 0;
  }
}

.search-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;
  border-bottom: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}

.header-content {
  flex: 1;
  margin-left: 16px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.search-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 18px;
  }
}

.search-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.search-form-container {
  background: white;
  padding: 24px;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-search-form {
  .form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
    margin-bottom: 20px;

    @media (max-width: 768px) {
      grid-template-columns: 1fr;
      gap: 16px;
    }
  }

  .form-item {
    margin-bottom: 0;

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: $text-regular;
    }
  }
}

.enhanced-input {
  width: 100%;

  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }
}

.form-actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    flex-direction: column;
  }
}

.search-btn {
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  border: none;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  color: white;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.reset-btn {
  border: 2px solid $border-color;
  color: $text-regular;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    border-color: $primary-color;
    color: $primary-color;
    background: $primary-light;
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.create-btn {
  border: 2px solid $primary-color;
  color: $primary-color;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    background: $primary-color;
    color: white;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 表格区域样式 */
.table-wrapper {
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;

  :deep(.el-card__body) {
    padding: 0;
  }
}

.table-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%);
  border-bottom: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
  }

  .header-icon {
    width: 40px;
    height: 40px;
    background: $primary-color;
    border-radius: $border-radius-small;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 18px;
    flex-shrink: 0;

    @media (max-width: 768px) {
      width: 36px;
      height: 36px;
      font-size: 16px;
    }
  }

  .header-content {
    flex: 1;
    margin-left: 12px;

    @media (max-width: 768px) {
      margin-left: 10px;
    }
  }

  .table-title {
    font-size: 16px;
    font-weight: 600;
    color: $text-primary;
    margin: 0 0 2px 0;

    @media (max-width: 768px) {
      font-size: 14px;
    }
  }

  .table-subtitle {
    font-size: 13px;
    color: $text-secondary;
    margin: 0;

    @media (max-width: 768px) {
      font-size: 12px;
    }
  }
}

.table-container {
  background: white;
  overflow-x: auto;
}

.enhanced-table {
  :deep(.el-table__header) {
    th {
      background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%) !important;
      color: $text-primary !important;
      font-weight: 600 !important;
      border-bottom: 2px solid $primary-light;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light !important;
      }

      td {
        padding: 14px 12px;
        border-bottom: 1px solid $border-light;
      }
    }

    .el-table__row--striped {
      background: #fafbff;

      &:hover {
        background: $primary-light !important;
      }
    }
  }

  // H5适配
  @media (max-width: 768px) {
    :deep(.el-table__header) th {
      padding: 12px 8px;
      font-size: 13px;
    }

    :deep(.el-table__body) td {
      padding: 12px 8px;
      font-size: 12px;
    }
  }
}

/* 表格单元格样式 */
.id-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.id-icon {
  color: $primary-color;
  font-size: 14px;
  flex-shrink: 0;
}

.id-text {
  font-weight: 500;
  color: $text-primary;
  font-family: 'Courier New', monospace;
}

.name-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.name-icon {
  color: $primary-color;
  font-size: 16px;
  flex-shrink: 0;
}

.name-text {
  font-weight: 600;
  color: $text-primary;
}

.status-cell {
  display: flex;
  justify-content: center;
  align-items: center;
}

.remark-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.remark-icon {
  color: $info-color;
  font-size: 14px;
  flex-shrink: 0;
}

.remark-text {
  color: $text-regular;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.4;
  max-width: 200px;
}

.time-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;
  font-size: 12px;
  color: $text-secondary;
}

.time-icon {
  color: $text-secondary;
  font-size: 14px;
}

.enhanced-dict-tag {
  border-radius: 6px;
  font-weight: 500;
  padding: 4px 8px;
  font-size: 12px;

  @media (max-width: 768px) {
    font-size: 11px;
    padding: 3px 6px;
  }
}

/* 操作按钮样式 */
.action-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 4px;
  }
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.3s ease;
  white-space: nowrap;

  &:hover {
    transform: scale(1.05);
  }

  .btn-icon {
    font-size: 12px;
  }

  @media (max-width: 768px) {
    padding: 4px 8px;
    font-size: 11px;
    justify-content: center;
  }

  &.edit-btn:hover {
    background: $primary-light;
    color: $primary-color;
  }

  &.detail-btn:hover {
    background: rgba(144, 147, 153, 0.1);
    color: $info-color;
  }

  &.delete-btn:hover {
    background: rgba(245, 108, 108, 0.1);
    color: $danger-color;
  }
}

/* 分页样式 */
.pagination-wrapper {
  display: flex;
  justify-content: center;
  padding: 20px 24px;
  background: white;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
  }

  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 6px;
      margin: 0 2px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }

      &.is-active {
        background: $primary-color;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 6px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }
    }

    @media (max-width: 768px) {
      .el-pagination__sizes,
      .el-pagination__jump {
        display: none;
      }
    }
  }
}

/* 弹窗样式 */
.enhanced-dialog {
  :deep(.el-dialog) {
    border-radius: $border-radius;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 0 auto;
    }
  }

  :deep(.el-dialog__header) {
    padding: 0;
    border-bottom: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__headerbtn) {
    top: 16px;
    right: 16px;
    z-index: 10;

    .el-dialog__close {
      color: white;
      font-size: 18px;

      &:hover {
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
}

.dialog-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }

  .header-icon {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: $border-radius-small;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    flex-shrink: 0;

    @media (max-width: 768px) {
      width: 36px;
      height: 36px;
      font-size: 16px;
    }
  }

  .header-content {
    flex: 1;
    margin-left: 12px;

    @media (max-width: 768px) {
      margin-left: 0;
    }
  }

  .dialog-title {
    font-size: 18px;
    font-weight: 600;
    margin: 0 0 4px 0;

    @media (max-width: 768px) {
      font-size: 16px;
    }
  }

  .dialog-subtitle {
    font-size: 13px;
    opacity: 0.9;
    margin: 0;

    @media (max-width: 768px) {
      font-size: 12px;
    }
  }
}

.form-preview-container {
  padding: 24px;
  background: white;
  min-height: 400px;
  max-height: 70vh;
  overflow-y: auto;

  @media (max-width: 768px) {
    padding: 16px;
    min-height: 300px;
    max-height: 60vh;
  }

  /* 滚动条美化 */
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 3px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .bpm-form-container {
    padding: 4px;
  }

  .search-form-container {
    padding: 12px;
  }

  .form-actions {
    gap: 8px;

    .el-button {
      padding: 8px 12px;
      font-size: 13px;
    }
  }

  .enhanced-table {
    :deep(.el-table__header) th {
      padding: 10px 6px;
      font-size: 12px;
    }

    :deep(.el-table__body) td {
      padding: 10px 6px;
      font-size: 11px;
    }
  }

  .pagination-wrapper {
    padding: 12px;
  }

  .remark-text {
    max-width: 120px;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .bpm-form-container {
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
  }

  .search-wrapper,
  .table-wrapper {
    background: #374151;
    border-color: #4b5563;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }

  .search-form-container,
  .table-container,
  .pagination-wrapper {
    background: #374151;
  }

  .table-header {
    background: #4b5563;
    border-color: #6b7280;
  }

  .search-title,
  .table-title {
    color: #f9fafb;
  }

  .name-text,
  .id-text {
    color: #f9fafb;
  }

  .remark-text {
    color: #d1d5db;
  }

  .enhanced-table {
    :deep(.el-table__header) {
      th {
        background: #4b5563 !important;
        color: #f9fafb !important;
        border-bottom-color: #6b7280;
      }
    }

    :deep(.el-table__cell) {
      border-bottom-color: #4b5563;
    }
  }

  .form-preview-container {
    background: #374151;
  }

  .enhanced-doc-alert {
    :deep(.el-alert) {
      background: #374151;
      border-color: #4b5563;
    }

    :deep(.el-alert__title) {
      color: #f9fafb;
    }

    :deep(.el-alert__icon) {
      color: #60a5fa;
    }
  }
}
</style>
